package cn.rengy.web.framework.wxmp.component;

import jakarta.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.rengy.web.framework.wxmp.component.auth.ComponentAuthService;
/**
 * 授权事件接收
 * @author rengy
 *
 */
@Controller
@RequestMapping(value = "/wxtp")
public class WechatComponentController{
	Logger logger = LoggerFactory.getLogger(WechatComponentController.class);
	@Autowired
	private WechatComponentService wechatComponentService;
	@Autowired
	private ComponentAuthService componentAuthService;
	/**
	 * pc端扫码授权
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/authView")
	public ModelAndView authView() throws Exception{
		String url=componentAuthService.getPcAuthURL();
		ModelAndView view=new ModelAndView();
		view.setViewName("/view/weixin/component/auth");
		view.addObject("authUrl", url);
		return view;
    }
	/**
	 * 移动端访问授权
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/redirectAuth")
	public String redirectAuth() throws Exception{
		return "redirect:"+componentAuthService.getWxAuthURL();
    }
	/**
	 * 授权事件接收入口
	 * 当公众号对第三方平台进行授权、取消授权、更新授权后，微信服务器会向第三方平台方的授权事件接收URL 推送相关通知。
	 * 微信服务器 每隔10分钟会向第三方的消息接收地址推送一次component_verify_ticket
	 * @param request
	 * @return
	 */
	@RequestMapping("/auth")
	@ResponseBody
	public String auth(HttpServletRequest request){
		return wechatComponentService.execute(request);
	}
	/**
	 * 用户同意授权后回调URI，得到授权码（authorization_code）和过期时间
	 * ?auth_code=xxx&expires_in=600
	 * @param request
	 * @return
	 */
	@RequestMapping("/authSuccess/{component_appid}")
	public ModelAndView authSuccess(@PathVariable("component_appid") String component_appid,@RequestParam("auth_code") String auth_code,@RequestParam("expires_in") Integer expires_in){
		ModelAndView view=new ModelAndView();
		view.setViewName("/view/weixin/component/auth_success");
		return view;
	}
	/**
	 * 消息与事件接收入口
	 */
	@RequestMapping("/{appid}/callback")
	@ResponseBody
	public void callback(@PathVariable("appid") String appid){
		
	}
			
			
}
